package com.zygh.policeasset.mapper;

import com.zygh.policeasset.pojo.QrCodeInfo;
import com.zygh.policeasset.pojo.SelectDistribution;
import com.zygh.policeasset.pojo.imports.QrCodeInfoImport;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface QrCodeInfoMapper {
    @Insert({"insert into qr_code_info(qr_code,qr_code_aluminum_id,qr_code_generated_id,type) " +
            "values(#{qrCode},#{qrCodeAluminumId},#{qrCodeGeneratedId},#{type})"})
    Integer addQrCodeInfo(QrCodeInfo qrCodeInfo);

    @Insert({"insert into qr_code_info(qr_code,qr_code_generated_id,name,address,management_unit,type,use_user) values(#{qrCode},#{qrCodeGeneratedId},#{name},#{address},#{managementUnit},#{type},#{useUser})"})
    Integer addQrCodeInfoImport(QrCodeInfoImport qrCodeInfoImport);

    @Delete({"delete from qr_code_info where qr_code_aluminum_id = #{qrCodeAluminumId}"})
    Integer deleteByQrCodeAluminumId(Integer num);

    @Delete({"delete from qr_code_info where id = #{id}"})
    Integer printQrCodeDelete(Integer num);

    @Delete({"delete from qr_code_info where qr_code_generated_id = #{qrCodeGeneratedId} and type = #{type}"})
    Integer deleteByQrCodeGeneratedId(@Param("qrCodeGeneratedId") Integer qrCodeGeneratedId,
                                      @Param("type") Integer type);

    @Select({"<script>select * from qr_code_info where qr_code_aluminum_id = #{qrCodeAluminumId} order by qr_num asc</script>"})
    @Results({@Result(property = "qrCode", column = "qr_code")})
    List<QrCodeInfo> selectListByQrCodeAluminumId(Integer num);

    @Select({"<script>select * from qr_code_info <where> " +
            "<if test='qrCodeGeneratedId != null'>  qr_code_generated_id = #{qrCodeGeneratedId} </if>" +
            "<if test='type != null '>  and type = #{type} </if></where> order by qr_num desc</script>"})
    @Results({@Result(property = "qrCode", column = "qr_code")})
    List<QrCodeInfo> selectListByQrCodeGeneratedId(@Param("qrCodeGeneratedId") Integer qrCodeGeneratedId,
                                                   @Param("type") Integer type);

    @Select({"<script>select * from qr_code_info " +
            "<where> " +
            "<if test='qrCodeGeneratedId != null'> qr_code_generated_id = #{qrCodeGeneratedId} </if>" +
            "<if test='type != null '>  and type = #{type} </if>" +
            "</where> " +
            "order by qr_num asc" +
            "</script>"})
    @Results({@Result(property = "qrCode", column = "qr_code"),
            @Result(property = "managementUnit", column = "management_unit"),
            @Result(property = "useUser", column = "use_user")})
    List<QrCodeInfoImport> findPrintQrCode(Integer qrCodeGeneratedId, Integer type);

    @Delete({"update qr_code_info set qr_code_generated_id = #{qrCodeGeneratedId} where qr_code = #{qrCode}"})
    Integer appUpdateQrCodeGeneratedId(@Param("qrCodeGeneratedId") Integer num, @Param("qrCode") String str);

    @Delete({"update qr_code_info set qr_code_generated_id = #{qrCodeGeneratedId} where id = #{id}"})
    Integer updateQrCodeGeneratedId(@Param("qrCodeGeneratedId") Integer num, @Param("id") Integer num2);

    @Delete({"update qr_code_info set qr_code_generated_id = NULL  where id = #{id}"})
    Integer updateQrCodeGeneratedIdIsNull(@Param("qrCodeGeneratedId") Integer num, @Param("id") Integer num2);

    @Select({"<script>select * from qr_code_info where qr_code_generated_id IS NULL and type is not null<if test='qrCode != null and qrCode.length &gt; 0'>  and qr_code = #{qrCode} </if> order by qr_num asc <if test='counter != 0 '>  limit ${counter} </if></script>"})
    @Results({@Result(property = "qrCode", column = "qr_code")})
    List<QrCodeInfo> undistributedQrCode(@Param("counter") Integer num, @Param("qrCode") String str);

    @Select({"select count(*) from qr_code_info where qr_code_generated_id IS NULL "})
    Integer undistributedSelect();

    @Update({"update qr_code_info set qr_num = #{qrNum} where qr_code = #{qrCode}"})
    Integer updateNum(@Param("qrNum") Integer num, @Param("qrCode") String str);

    @Select({"select count(*) from qr_code_info where qr_code = #{qrCode} and qr_code_generated_id is not null"})
    Integer checkQrCode(String str);

    @Select({"<script>" +
            "SELECT  " +
            " u.name as userName, " +
            " u.id as userId, " +
            " IFNULL( a.count, 0 ) as count " +
            "FROM " +
            " `user` u " +
            " LEFT JOIN ( " +
            " SELECT " +
            "  u.NAME AS userName, " +
            "  IFNULL( count( qci.user_id ), 0 ) AS count  " +
            " FROM " +
            " `user` u " +
            " LEFT JOIN qr_code_info qci ON qci.user_id = u.id  " +
            " AND qci.qr_code_generated_id = 0  " +
            " AND qci.user_id IS NOT NULL  " +
            " GROUP BY " +
            " qci.user_id " +
            " ORDER BY " +
            "  count(*) DESC " +
            " ) AS a ON u.name = a.userName " +
            "<where> <if test='qrCodeGeneratedId != null'>  u.NAME like concat('%',#{userName},'%') </if></where>" +
            " order by count desc " +
            "</script>"})
    List<SelectDistribution> selectDistribution(String userName);

    @Select({"select qr_code from qr_code_info where user_id = #{userId} "})
    List<String> selectDistributionByUserId(Integer userId);

    @Update({"<script>UPDATE qr_code_info set qr_code_generated_id=0,user_id = #{userId} " +
            "where qr_code IN " +
            "<foreach collection='qrCodes' item='item' index='index' open='(' separator=',' close=')'>" +
            " #{item} </foreach>" +
            "</script>"})
    Integer bingDistribution(@Param("qrCodes") String[] qrCodes, @Param("userId") Integer userId);

}